package com.xxgc.vfc.modules.provider.info.service;

import com.xxgc.vfc.api.dto.SchoolDTO;
import com.xxgc.vfc.api.service.ISchoolService;
import com.xxgc.vfc.api.vo.SchoolVO;
import com.xxgc.vfc.common.redis.util.RedisUtil;
import com.xxgc.vfc.modules.provider.info.entity.School;
import com.xxgc.vfc.modules.provider.info.mapper.SchoolMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: SJY
 * @Date: 2023/11/7 16:43:25
 */
@Slf4j
@DubboService
public class SchoolServiceImpl implements ISchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean addSchool(SchoolVO schoolVO) {
        School school = new School();
        BeanUtils.copyProperties(schoolVO,school);
        schoolMapper.insert(school);
        log.info(schoolVO.toString());
        return true;
    }

    @Override
    public SchoolDTO getSchoolById(Integer id) {
        String key = "select_school_"+id;
        //先判断redis有没有，如果有就直接返回
        if(redisUtil.hasKey(key)){
            SchoolDTO schoolDTO = (SchoolDTO) redisUtil.get(key);
            log.info("-----------缓存-------------");
            return schoolDTO;
        }else{
            //如果没有就查数据库然后放入到redis中
            School school = schoolMapper.selectById(id);
            SchoolDTO schoolDTO = new SchoolDTO();
            BeanUtils.copyProperties(school,schoolDTO);
            log.info("----------数据库--------------");
            redisUtil.set(key,schoolDTO,60*60*12);
            return schoolDTO;
        }
    }


    //修改方法时跟新
    //在重新进入系统时跟新
    //时间过期时跟新

}
